﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MacomberMapSystem.Common.CIM;
using MacomberMapSystem.Common.Integration;
using System.ComponentModel;
using System.Xml;
using MacomberMapSystem.Common.Database;
using MacomberMapSystem.Common.Serialization;
using System.Drawing;

namespace MacomberMapSystem.Common.User_Interfaces.One_Lines
{
    /// <summary>
    /// This class holds information on a transformer winding
    /// </summary>
    public class MM_OneLine_TransformerWinding
    {
        #region Variable declarations
        /// <summary>Our base element for the one-line component</summary>
        public CIM_Element CIM;

        /// <summary>The orientation of our element</summary>
        public MM_OneLine_Element.enumOrientations Orientation
        {
            get { return _Orientation; }
            set
            {
                _Orientation = value;
                MM_Serializable.WriteAttribute(Definition, "Orientation", value);
            }
        }

        private MM_OneLine_Element.enumOrientations _Orientation = MM_OneLine_Element.enumOrientations.Unknown;

        /// <summary>The KV level of our element</summary>
        public MM_KVLevel KVLevel;

        /// <summary>The bounds of the winding within the transformer rectangle</summary>
        [Category("Display"), Description("The bounds of the winding within the transformer rectangle")]
        public Rectangle Bounds
        {
            get { return _Bounds; }
            set
            {
                _Bounds = value;
                MM_Serializable.WriteAttribute(Definition, "Bounds", value);
            }
        }
        private Rectangle _Bounds;


        /// <summary>The Visible of the winding within the transformer rectangle</summary>
        [Category("Display"), Description("Whether the winding is visible"), DefaultValue(true)]
        public bool Visible
        {
            get { return _Visible; }
            set
            {
                _Visible = value;
                MM_Serializable.WriteAttribute(Definition, "Visible", value);
            }
        }
        private bool _Visible = true;


        /// <summary>Whether the winding is the primary one</summary>
        [Category("Display"), Description("Whether the winding is the primary one"), DefaultValue(false)]
        public bool IsPrimary
        {
            get { return _IsPrimary; }
            set
            {
                _IsPrimary = value;
                MM_Serializable.WriteAttribute(Definition, "BaseElement.IsPrimary", value);
            }
        }
        private bool _IsPrimary = false;

        /// <summary>Whether the winding has a phase shifter</summary>
        [Category("Display"), Description("Whether the winding has a phase shifter"), DefaultValue(false)]
        public bool IsPhaseShifter
        {
            get { return _IsPhaseShifter; }
            set
            {
                _IsPhaseShifter = value;
                MM_Serializable.WriteAttribute(Definition, "BaseElement.IsPhaseShifter", value);
            }
        }
        private bool _IsPhaseShifter = false;


        /// <summary>The node associated with our winding</summary>
        public CIM_Element AssociatedNode;

        /// <summary>The definition for the transformer winding</summary>
        public XmlElement Definition;


        #endregion

        #region Initialization
        /// <summary>
        /// Initialize a new transfomrer winding
        /// </summary>
        /// <param name="OneLine"></param>
        /// <param name="CIM"></param>
        public MM_OneLine_TransformerWinding(XmlElement OneLine, MM_Database_Model CIM)
        {
            this.Definition = OneLine;
            MM_Serializable.InitializeFromXml(this, OneLine, CIM);

            //Locate our element in CIM
            if (OneLine.HasAttribute("rdfID"))
                this.CIM = CIM.FromRdfID(OneLine.Attributes["rdfID"].Value);
            else if (OneLine.HasAttribute("BaseElement.TEID"))
            {
                this.CIM = CIM.FromTEID(Convert.ToUInt64(OneLine.Attributes["BaseElement.TEID"].Value));

                //Add in our rdf:ID
                if (this.CIM != null)
                    MM_Serializable.WriteAttribute(OneLine, "rdfID", this.CIM.rdfID);
            }

            if (OneLine.HasAttribute("BaseElement.IsPrimary"))
                IsPrimary = XmlConvert.ToBoolean(OneLine.Attributes["BaseElement.IsPrimary"].Value);
            else if (this.CIM != null)
                IsPrimary = this.CIM.Attributes["cim:TransformerWinding.windingType"].EndsWith("primary");

            if (OneLine.ParentNode.Attributes["BaseElement.PhaseShifter"] != null && XmlConvert.ToBoolean(OneLine.ParentNode.Attributes["BaseElement.PhaseShifter"].Value))
                IsPhaseShifter = true;


            if (this.CIM != null)
                this.KVLevel = Data_Manager.LocateKVLevel(this.CIM.VoltageLevel);
        }
        #endregion
    }
}